﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Bon.it
{
    public static class InputHandlers
    {
        #region Decimal text box
        public static void DecimalTextChangedEventHandler(Object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            Int32 selectionStart = textBox.SelectionStart;

            String newText = textBox.Text.Where(c => Char.IsDigit(c) || Char.IsControl(c) || c.ToString() == System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator).Aggregate(String.Empty, (current, c) => current + c);

            textBox.Text = newText;

            textBox.SelectionStart = selectionStart <= textBox.Text.Length ?
                selectionStart : textBox.Text.Length;
        }


        // Use the PreviewTextInputHandler to respond to key presses 
        public static void DecimalPreviewTextInputHandler(Object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsTextAllowed(e.Text);
        }

        // Use the DataObject.Pasting Handler  
        public static void DecimalPastingHandler(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsTextAllowed(text)) e.CancelCommand();
            }
            else e.CancelCommand();
        }

        

        #endregion
        
        #region Numeric Textbox

        public static void NumericTextChangedEventHandler(Object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            Int32 selectionStart = textBox.SelectionStart;

            String newText = textBox.Text.Where(c => Char.IsDigit(c) || Char.IsControl(c)).Aggregate(String.Empty, (current, c) => current + c);

            textBox.Text = newText;

            textBox.SelectionStart = selectionStart <= textBox.Text.Length ?
                selectionStart : textBox.Text.Length;
        }


        // Use the PreviewTextInputHandler to respond to key presses 
        public static void NumericPreviewTextInputHandler(Object sender, TextCompositionEventArgs e)
        {
            e.Handled = !IsTextAllowed(e.Text);
        }

        // Use the DataObject.Pasting Handler  
        public static void NumericPastingHandler(object sender, DataObjectPastingEventArgs e)
        {
            if (e.DataObject.GetDataPresent(typeof(String)))
            {
                String text = (String)e.DataObject.GetData(typeof(String));
                if (!IsTextAllowed(text)) e.CancelCommand();
            }
            else e.CancelCommand();
        }

        #endregion


        #region private methods
        private static Boolean IsTextAllowed(String text)
        {
            return Array.TrueForAll(text.ToCharArray(),
                delegate(Char c) { return Char.IsDigit(c) || Char.IsControl(c); });
        }
        #endregion

    }
}
